import axios from "axios";
import qs from "qs";
import {
    ElNotification
} from 'element-plus'
// // axios.defaults.baseURL = ''  //正式
// axios.defaults.baseURL = 'http://localhost:3000' //测试

// //post请求头
axios.defaults.headers.post["Content-Type"] = "application/x-www-form-urlencoded;charset=UTF-8";
// //允许跨域携带cookie信息
axios.defaults.withCredentials = true;
// //设置超时
axios.defaults.timeout = 15000;

axios.interceptors.request.use(
    config => {
        config.headers['Authorization'] = 'c6zbgdw34ner6yq0';
        config.headers['Content-Type'] = 'application/json';
        //   config.headers['Content-Type'] = 'Authorization';
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);

axios.interceptors.response.use(
    response => {
        if (response.status == 200) {
            return Promise.resolve(response);
        } else {

            ElNotification({
                title: '请求异常',
                message: JSON.stringify(response),
                type: 'success',
            })
            return Promise.reject(new Error('请求异常'));

        }
    },
    error => {
        ElNotification({
            title: '请求异常',
            message: JSON.stringify(response),
            type: 'success',
        })
        window.$message.error("请求异常:" + JSON.stringify(error));


    }
);
export default {
    /**
     * @param {String} url 
     * @param {Object} data 
     * @returns Promise
     */
    post(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'post',
                    url,
                    data: qs.stringify(data),
                })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                });
        })
    },
    post2(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'post',
                    url,
                    data: data,
                })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                });
        })
    },
    get(url, data) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'get',
                    url,
                    params: data,
                })
                .then(res => {
                    console.log(url)
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    },
    getx(url, data, responseType) {
        return new Promise((resolve, reject) => {
            axios({
                    method: 'get',
                    url,
                    responseType: responseType,
                    params: data,
                })
                .then(res => {
                    resolve(res.data)
                })
                .catch(err => {
                    reject(err)
                })
        })
    }
};